package com.bgrconsulting.stockorderentry.service.rest.impl;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

import com.bgrconsulting.stockorderentry.service.converter.PlaceChangeEquityOrderConverter;
import com.bgrconsulting.stockorderentry.service.converter.PreviewEquityOrderConverter;
import com.bgrconsulting.stockorderentry.service.converter.response.PlaceChangeEquityOrderResponseConverter;
import com.bgrconsulting.stockorderentry.service.converter.response.PlaceEquityOrderResponseConverter;
import com.bgrconsulting.stockorderentry.service.dao.PlaceChangeEquityOrderResponseDao;
import com.bgrconsulting.stockorderentry.service.dao.PlaceEquityOrderResponseDao;
import com.bgrconsulting.stockorderentry.service.dao.PreviewEquityOrderResponseDao;
import com.bgrconsulting.stockorderentry.service.model.response.EquityOrderResponse;
import com.bgrconsulting.stockorderentry.service.model.response.PlaceChangeEquityOrderResponse;

@Path("/order/rest/placechangeequityorder/")
public class PlaceChangeEquityOrderResource {
	@Context
	UriInfo uriInfo;
	@Context
	Request request;
	String id;
	/*public PreviewEquityOrderResource(UriInfo uriInfo, Request request, String id) {
		this.uriInfo = uriInfo;
		this.request = request;
		this.id = id;
	}*/
	
	//Application integration 		
	@GET
	@Path("/{orderId}") 
	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	public PlaceChangeEquityOrderResponseConverter getPlaceChangeEquityOrderResponse(@PathParam ("orderId") String orderId) {
		PlaceChangeEquityOrderResponseConverter placeChangeEquityOrderReqponse = PlaceChangeEquityOrderResponseDao.instance.getModel().get(orderId);
		if(placeChangeEquityOrderReqponse==null)
			throw new RuntimeException("Get: Placed equity order with " + orderId +  " not found");
		return placeChangeEquityOrderReqponse;
	}

	@POST
    @Consumes({"application/xml","application/json"})
    public Response createNewPlaceEquityOrder(JAXBElement<PlaceChangeEquityOrderConverter> placeChangeEquityOrder) {
        
		PlaceChangeEquityOrderResponse placeEquityOrderResponse = new PlaceChangeEquityOrderResponse();
		
		PlaceChangeEquityOrderConverter peoc = placeChangeEquityOrder.getValue();
		
		EquityOrderResponse equityOrderResponse = new EquityOrderResponse();
		
		placeEquityOrderResponse.setEquityOrderResponse(equityOrderResponse);
		
	
		PlaceChangeEquityOrderResponseConverter peorc = new PlaceChangeEquityOrderResponseConverter(placeEquityOrderResponse);
		
		return putAndGetResponse(peorc);
		
    }
	
	private Response putAndGetResponse(PlaceChangeEquityOrderResponseConverter placeChangeEquityOrderResponse) {
		Response res;
		if(PlaceChangeEquityOrderResponseDao.instance.getModel().containsKey(placeChangeEquityOrderResponse.getEquityOrderResponse().getOrderNum())) {
			res = Response.noContent().build();
		} else {
			res = Response.created(uriInfo.getAbsolutePath()).build();
		}
		PlaceChangeEquityOrderResponseDao.instance.getModel().put(placeChangeEquityOrderResponse.getEquityOrderResponse().getOrderNum(), placeChangeEquityOrderResponse);
		return res;
	}
}


